﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Xml.Linq;
using System.Linq.Expressions;

namespace GenY.TypeWrapper.Observable {
  [TypeWapper( "Observable" )]
  public partial class Observable : ITypeWrapper {
    Dictionary<Type, IEnumerable<MemberInfo>> Targets { get; set; }

    public string Wrap( Dictionary<Type, IEnumerable<MemberInfo>> targets, XElement GenYElement ) {
      Targets = targets;
      return this.TransformText( );
    }

    public bool IsSubClassOfEventArgs( EventInfo e ) {
      return e.EventHandlerType.GetMethod( "Invoke" ).GetParameters( ).Last( ).ParameterType.IsSubclassOf( typeof( EventArgs ) );
    }

    public Type EventArgType( EventInfo e ) {
      return e.EventHandlerType.GetMethod( "Invoke" ).GetParameters( ).Last( ).ParameterType;
    }

    public bool SupportsChangeEvents( object target, String propertyName ) {
      return System.ComponentModel.TypeDescriptor.GetProperties( target )
          .OfType<System.ComponentModel.PropertyDescriptor>( )
          .Where( p => p.Name == propertyName ).First( ).SupportsChangeEvents;
    }


  }
}